package com.young.mylib.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.alibaba.fastjson.JSONObject;
import com.young.mylib.R;
import com.young.mylib.utils.ConverterUtils;
import com.young.mylib.view.edittext.LineEditText;


/**
 * Created by Young on 2017/9/6.
 */
public class MultiEditTextDialog extends AlertDialog implements View.OnClickListener {
    private final static String TAG = "MultiEditTextDialog";
    private int pos = 0;
    private TextView tvStatus;
    private LineEditText etPrice;
    private LineEditText etName;

    private TextView submitTxt;
    private TextView tvUp;
    private TextView tvDown;

    private Context mContext;

    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;
    private String[] data;
    private JSONObject price;
    private String name;
    private int enableText, disableText;

    public MultiEditTextDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public MultiEditTextDialog(Context context, int themeResId, String title) {
        super(context, themeResId);
        this.mContext = context;
        this.title = title;
    }

    public MultiEditTextDialog(Context context, int themeResId, String title, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.title = title;
        this.listener = listener;
    }

    public MultiEditTextDialog(Context context, int themeResId, String title, String[] data, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.title = title;
        this.data = data;
        this.listener = listener;
    }

    protected MultiEditTextDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public MultiEditTextDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public MultiEditTextDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    public MultiEditTextDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    public MultiEditTextDialog setData(String[] data) {
        this.data = data;
        return this;
    }

    public MultiEditTextDialog setPriceJson(JSONObject price) {
        this.price = price;
        return this;
    }

    public MultiEditTextDialog setName(String name) {
        this.name = name;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_multi_edittext);
        setCanceledOnTouchOutside(true);

        initView();

    }

    private void initView() {
        Log.i(TAG, "initView: " + data.length);
        enableText = mContext.getResources().getColor(R.color.bg_commom);
        disableText = mContext.getResources().getColor(R.color.font_tab_0);
        tvStatus = (TextView) findViewById(R.id.tvStatus);
        etPrice = (LineEditText) findViewById(R.id.etPrice);
        etPrice.setSingleLine();
        etPrice.setHint("0");
        etName = (LineEditText) findViewById(R.id.etName);
        etName.setSingleLine();
        etName.setHint("设置产品名称");

        submitTxt = (TextView) findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        tvUp = (TextView) findViewById(R.id.tvUp);
        tvUp.setOnClickListener(this);
        tvDown = (TextView) findViewById(R.id.tvDown);
        tvDown.setOnClickListener(this);

        if (!TextUtils.isEmpty(positiveName)) {
            submitTxt.setText(positiveName);
        }
        if (!TextUtils.isEmpty(name)) {
            etName.setText(name);
        }
//        if (!TextUtils.isEmpty(negativeName)) {
//            tvUp.setText(negativeName);
//        }

        if (data != null && data.length > 0) {
            tvStatus.setText(data[pos] + "价：");
        }
        tvUp.setEnabled(false);
        if (price == null) {
            price = new JSONObject();
            for (String s : data) {
                price.put(s, 0);
            }
        }
        Log.i(TAG, price.toString());
        try {
            int num = ConverterUtils.str2int(price.get(data[pos]).toString(), 0);
            if (num != 0) {
                etPrice.setText(num + "");
            } else {
                etPrice.setText("");
            }

        } catch (Exception e) {

        }

        //只用下面这一行弹出对话框时需要点击输入框才能弹出软键盘
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//加上下面这一行弹出对话框时软键盘随之弹出
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tvUp) {
            int productPrice = ConverterUtils.str2int(etPrice.getText().toString(), 0);
            price.put(data[pos], productPrice);
            pos--;
            tvStatus.setText(data[pos] + "价：");
            productPrice = 0;

            productPrice = price.getIntValue(data[pos]);

            if (productPrice == 0) {
                etPrice.setText("");
            } else {
                etPrice.setText(productPrice + "");
            }
            tvDown.setEnabled(true);
            tvDown.setTextColor(enableText);
            if (pos == 0) {
                tvUp.setEnabled(false);
                tvUp.setTextColor(disableText);
            }
        }
        if (v.getId() == R.id.tvDown) {
            int productPrice = ConverterUtils.str2int(etPrice.getText().toString(), 0);

            price.put(data[pos], productPrice);

            pos++;
            productPrice = 0;

            productPrice = price.getIntValue(data[pos]);

            if (productPrice == 0) {
                etPrice.setText("");
            } else {
                etPrice.setText(productPrice + "");
            }
            tvStatus.setText(data[pos] + "价：");
            tvUp.setEnabled(true);
            tvUp.setTextColor(enableText);
            Log.i(TAG, pos + "-->" + data.length);
            if (pos == (data.length - 1)) {
                Log.i(TAG, "tvDown is disabled");
                tvDown.setEnabled(false);
                tvDown.setTextColor(disableText);
            }
        }
        if (v.getId() == R.id.submit) {
            int productPrice = ConverterUtils.str2int(etPrice.getText().toString(), 0);
            price.put(data[pos], productPrice);
            if (TextUtils.isEmpty(etName.getText().toString())) {
                Toast.makeText(mContext, "请设置产品名称", Toast.LENGTH_SHORT).show();
                return;
            }
            if (listener != null) {
                listener.onClick(this, etName.getText().toString(), price);
            }
            this.dismiss();
        }

    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, String name, JSONObject price);
    }
}